<template>
  <div class="main">
    <div class="top">
      <div class="change" @click="isMap=!isMap">{{isMap ? '切换到列表' : '切换到地图'}}</div>
      <div class="select" @click="popShow=!popShow">{{prt_tags}}<span></span></div>
      <div class="selset_list" v-if="popShow" position="bottom" style="width: 100%;">
        <ul>
          <li v-for="item in dataList" @click="channge_select(item.tag,item.name)">{{item.name}}</li>
        </ul>
      </div>
      <div class="searchs">
        <input v-model="keyWord" placeholder="搜索机构名称"/>
      </div>
    </div>
    <div v-show="!isMap" class="ListMain" v-infinite-scroll="loadMore" infinite-scroll-disabled="loading"
         infinite-scroll-distance="10">
      <div class="item" v-for="item in data" @click="toDetail(item)">
        <div class="left">
          <div class="title">{{item.akb021}}</div>
          <div class="address">地址：{{item.aae006}}</div>
        </div>
        <div class="distance"></div>
      </div>
    </div>
    <div v-show="isMap">
      <div id="myMap" class="myMapClass" ref="myMap"></div>
      <div class="pop" v-show="popMap">
        <ul>
          <li class="special" @click="toDetail(popData)">{{popData.akb021}}</li>
          <li><label>电话</label>{{popData.aae005}}</li>
          <li><label>地址</label>{{popData.aae006}}</li>
        </ul>
        <div class="btn_group"><a :href="'tel:'+ popData.aae005">电话</a><a
          :href="'epmap://navi?lon='+popData.lng+'&lat='+popData.lat+'&type=baidumap'">导航</a></div>
      </div>
    </div>
  </div>
</template>

<script>
  import BMap from 'BMap'
  export default {
    data () {
      return {
        prt_tags: "所有机构",
        popShow: false,
        dataList: [
          {name: "所有机构", tag: ''},
          {name: "定点医院", tag: '1'},
          {name: "定点药店", tag: '2'},
        ],
        data: {},
        data1: {},
        index: "0",
        loading: true,
        location: '',
        keyWord: "",
        popMap: false,
        popData: {},
        isMap: true,
        type: "",
        currentLng: "",
        currentLat: ""
      }
    },
    methods: {
      channge_select(a, b){
        this.prt_tags = b;
        this.popShow = false;
        this.type = a;
        this.getData();
      },
      //获取定点医院数据
      getData(){
        this.$http.post("transmit/noauth", {
          "transmitUrl": "/simis/hospitalpharmacy/queryHospitalPharmacyListForApp",
          "method": "get",
          "objectId": "a0693c1368a3429097dd0725a35d6898",
          "objectType": "1",
          "akb021": this.keyWord,
          "lng":this.currentLng,
          "lat": this.currentLat,
          "akb022": this.type
        }).then(res => {
          console.log(res)
          if (res.success) {
            this.data = res.body.rows;
            this.addMarker();
          }
        });
      },
      toDetail(item){
        this.$router.push({path: "/jgDetail", query: {item: JSON.stringify(item)}});
      },
      loadMore(){
      },
      initMap () {
        this.createMap();
        this.addMapControl();
      },
      //创建地图
      createMap(){
        var map = new BMap.Map(this.$refs.myMap);//在百度地图容器中创建一个地图
        var point = new BMap.Point(this.currentLng,this.currentLat);//定义一个中心点坐标
        map.centerAndZoom(point, 13);//设定地图的中心点和坐标并将地图显示在地图容器中
        window.map = map;//将map变量存储在全局
      },
      //向地图添加控件
      addMapControl(){
        //向地图中添加缩放控件
        var ctrl_nav = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_LARGE});
        map.addControl(ctrl_nav);
        //向地图中添加缩略图控件
        var ctrl_ove = new BMap.OverviewMapControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, isOpen: 1});
        map.addControl(ctrl_ove);
        //向地图中添加比例尺控件
        var ctrl_sca = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});
        map.addControl(ctrl_sca);
      },
      //向地图中添加marker
      addMarker(){
        window.map.clearOverlays();
        let _this = this;
        var point = new Array();//定义数组标注经纬信息
        var marker = new Array();//定义数组点对象信息
        let data = _this.data;
        for (var i = 0; i < data.length; i++) {//遍历
          var icons = "";
          if (data[i].akb022 == 1) {
            icons = new window.BMap.Icon(require("../../assets/images/icon/icon-city.png"), new window.BMap.Size(20, 20), {
              imageSize: new window.BMap.Size(20, 20)
            });
          } else if (data[i].akb022 == 2) {
            icons = new window.BMap.Icon(require("../../assets/images/icon/icon-fail.png"), new window.BMap.Size(20, 20), {
              imageSize: new window.BMap.Size(20, 20)
            });
          }
          point[i] = new window.BMap.Point(data[i].lng, data[i].lat);
          marker[i] = new window.BMap.Marker(point[i], {icon: icons, name: "111"});
          marker[i].customData = {"item": data[i]};
          window.map.addOverlay(marker[i]);
          (function () {
            marker[i].addEventListener("click", function (e) {
              _this.popData = {};
              _this.popData = e.target.customData.item;
              _this.popMap = true;
            });
          })();
        }
      },
      getPosition(res){
        if (res.success) {
          this.currentLng = res.body.longitude;
          this.currentLat = res.body.latitude;
          this.getData();
        } else {
          this.$toast({
            message: "定位获取失败",
            duration: 3000
          });
        }
      }
    },
    watch: {
      keyWord(){
        this.getData();
      }
    },
    created(){
      window.getPosition=this.getPosition();
      if (window.isAndroid) {
        window.EPApi.getLocation("getPosition");
      }else if(window.isiOS){
        window.webkit.messageHandlers.getLocation.postMessage("getPosition");
      }
    },
    mounted(){
      this.initMap();
    },
  }
</script>

<style lang="less" scoped>
  .top {
    width: 100%;
    position: fixed;
    left: 0;
    top: 0;
    background: #FFFFFF;
    z-index: 1;
    .change {
      line-height: 0.45rem;
      font-size: 0.14rem;
      border-bottom: 0.01rem solid #DFDFDF;
      background: url("../../assets/images/icon/icon-jtdown.png") no-repeat right 4% center;
      background-size: 0.15rem;
      padding-left: 4%;
      color: #0485e0;
    }
  }

  .select {
    line-height: 0.45rem;
    font-size: 0.14rem;
    border-bottom: 0.01rem solid #DFDFDF;
    background: url("../../assets/images/icon/icon-jtdown.png") no-repeat right 4% center;
    background-size: 0.15rem;
    padding-left: 4%;
  }

  .selset_list {
    position: absolute;
    top: 0.92rem;

    ul {
      width: 100%;
    }

    li {
      line-height: 0.45rem;
      font-size: 0.14rem;
      background: #ffffff;
      width: 100%;
      padding-left: 4%;
    }

    li:not(:last-child) {
      border-bottom: 1px solid #DFDFDF;
    }

    > div {
      line-height: 50px;
      text-align: center;
      margin-top: 10px;
      background: #ffffff;
      font-size: 16px;
      color: #3399FF;
    }
  }

  .item {
    background: #FFFFFF;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    padding: 15px 15px;
    padding-right: 0;
  }

  .item {
    border-bottom: 1px solid #DFDFDF;
  }

  .item .left {
    max-width: 65%;
    line-height: 25px;
  }

  .item .left .title {
    font-size: 16px;
    color: #353535;
  }

  .item .left .address {
    font-size: 12px;
    color: #888888;
  }

  .item img {
    height: 10px;
    display: inline;
    width: 10px;
  }

  .item .distance {
    background: url(../../assets/images/right.png) no-repeat right center;
    padding-right: 15px;
    color: #888888;
    width: 0.15rem;
    height: 0.15rem;
  }

  .searchs {
    background: #DFDFDF;
    text-align: center;
    width: 92%;
    border-radius: 6px;
    margin: 0.1rem auto;
  }

  .searchs input {
    width: 70%;
    background: #DFDFDF url("../../assets/images/icon/icon-magnifier.png") no-repeat left 5px center;
    background-size: 15px;
    border: none;
    line-height: 0.45rem;
    padding-left: 30px;
  }

  .myMapClass {
    width: 100%;
    height: calc(100vh - 1.55rem);
    margin-top: 1.55rem;
  }

  .pop {
    width: 90%;
    position: fixed;
    z-index: 1;
    bottom: 0.1rem;
    left: 5%;
    background: #ffffff;
    border-radius: 0.1rem;
    ul {
      padding: 0.1rem 0.2rem;
      li {
        padding: 0.05rem 0;
        font-size: 0.12rem;
        color: #999;
        label {
          margin-right: 0.1rem;
        }
      }
      .special {
        font-size: 0.15rem !important;
        color: #333 !important;
        font-weight: bold;
        background: url("../../assets/images/icon/icon-dhjt.png") no-repeat right center;
        background-size: 0.18rem;
      }
    }
    .btn_group {
      border-top: 0.01rem solid #DFDFDF;
      line-height: 0.45rem;
      text-align: center;
      a {
        display: inline-block;
        width: 50%;
        box-sizing: border-box;
        color: #0485e0;
      }
      a:first-child {
        border-right: 0.01rem solid #DFDFDF;
      }
    }
  }

  .ListMain {
    margin-top: 1.55rem
  }
</style>
